import {_decorator, Button, Component, director, instantiate, Node, Prefab, ResolutionPolicy, Vec3, view} from 'cc';
import {lobby} from "db://assets/script/lobby";
import {PropBag} from "db://assets/script/game/mainui/props/prop-bag";
import Long from "long";
import {BattleInGame} from "db://assets/script/game/battle/battle-in-game";
import {GameEvent} from "db://assets/script/game-event";
import {ChangeMainUiTagEvent} from "db://assets/script/common/events/ui-events/change-main-ui-tag-event";
import {userInfo} from "db://assets/script/user-info";
import {PinchZoom} from "db://assets/script/common/pinch-zoom";

const {ccclass, property} = _decorator;

@ccclass('MainUI')
export class MainUI extends Component {

    @property(Button)
    switchCityBtn!: Button;

    @property(Button)
    switchBattleBtn!: Button;

    @property(Prefab)
    battleInGamePref!: Prefab

    @property(Prefab)
    cityFieldPref!: Prefab

    @property(Node)
    uiInBattle!: Node;

    @property(Node)
    uiInCity!: Node;

    @property(PropBag)
    propBag!: PropBag

    battleInGame: Node | null = null
    cityField: Node | null = null

    tag: string = 'city';

    start() {
        view.setResolutionPolicy(ResolutionPolicy.SHOW_ALL);
        this._updateUI(null)
        this.propBag.node.active = false
        this.switchBattleBtn.node.on(Button.EventType.CLICK, () => {
            if (userInfo.cityInfo.checkInEdit()) userInfo.cityInfo.changeInEdit()
            lobby.instance.showChangeMask(() => {
                this.tag = 'battlefield'
                this._updateUI(null)
            })

        })
        this.switchCityBtn.node.on(Button.EventType.CLICK, () => {
            lobby.instance.showChangeMask(() => {
                this.tag = 'city'
                this._updateUI(null)
            })
        })
    }

    protected onLoad() {
        this.onEvent()
    }

    onEvent() {
        director.on(GameEvent.uiChangeMainUiTag, this._changeTag, this)
    }

    offEvent() {
        director.off(GameEvent.uiChangeMainUiTag, this._changeTag, this)
    }

    private _tryCreateBattleInGame(fishId: Long | null) {
        if (this.battleInGame?.isValid) return
        const battleInGame = instantiate(this.battleInGamePref)
        this.battleInGame = battleInGame
        lobby.instance.map.addChild(battleInGame)
        battleInGame.getComponent(BattleInGame)!.init(fishId)
    }

    private _tryDeleteBattleInGame() {
        if (!this.battleInGame) return
        this.battleInGame!.destroy()
        this.battleInGame = null
    }

    private _tryCreateCityField() {
        if (this.cityField?.isValid) return
        const cityField = instantiate(this.cityFieldPref)
        this.cityField = cityField
        lobby.instance.city.addChild(cityField)
    }

    private _tryDeleteCityField() {
        if (!this.cityField) return
        this.cityField!.destroy()
        this.cityField = null
    }

    private _changeTag(event: ChangeMainUiTagEvent) {
        if (event.tag == this.tag) return
        lobby.instance.showChangeMask(() => {
            this.tag = event.tag
            this._updateUI(event.params['fish-id'])
        })
    }

    _updateUI(fishId: Long | null) {
        if (this.tag == 'city') {
            this.uiInBattle.active = false
            this.uiInCity.active = true
            this._tryDeleteBattleInGame()
            this._tryCreateCityField()
        } else {
            this.uiInBattle.active = true
            this.uiInCity.active = false
            this._tryDeleteCityField()
            this._tryCreateBattleInGame(fishId)
        }
        lobby.instance.clearAllTempUI()
        const mc = lobby.instance.getMapCamera()
        lobby.instance.resetMapCameraHeight()
        mc.node.setPosition(new Vec3(0, 0, 1000))
    }

    update(deltaTime: number) {

    }

    protected onDestroy() {
        this.offEvent()
    }
}


